package com.pyramid.order;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pyramid.R;
import com.pyramid.adapter.SomePagerAdapter;
import com.pyramid.main.DefaultActivity;
import com.pyramid.main.PyramidManageApplication;
import com.pyramid.tools.Tools;

public class OrderTwoActivity extends DefaultActivity implements
		OnClickListener {
	Button butNo1, butNo2, butNo3, butNo4, butNo5, butNo6, butNo7, butNo8,
			butLh, butZh;
	AlertDialog alertDialog;
	View bodyView;
	ViewPager viewPager;
	ArrayList<View> listView;
	SomePagerAdapter somePagerAdapter;
	LinearLayout linearList;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
	}

	private void init() {
		// top left button
		butTopLeft.setVisibility(View.VISIBLE);
		butTopLeft.setBackgroundResource(R.drawable.back);
		butTopLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		// top right button
		butTopRight.setVisibility(View.VISIBLE);
		butTopRight.setBackgroundResource(R.drawable.home);
		butTopRight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		// bottom left button
		butBotLeft.setVisibility(View.VISIBLE);
		butBotLeft.setBackgroundResource(R.drawable.calculate);
		butBotLeft.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				startActivity(new Intent(OrderTwoActivity.this,
						CountToRepActivity.class));
			}
		});
		// arrow
		imageArrow.setVisibility(View.VISIBLE);
		// title
		imageTitle.setBackgroundResource(R.drawable.title_chongqing);
		imageTitle.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				alertDialog = new AlertDialog.Builder(OrderTwoActivity.this)
						.create();
				alertDialog.setView(getLayoutInflater().inflate(
						R.layout.order_titledialog, null));
				alertDialog.show();
				Tools.dialogwidth(alertDialog, OrderTwoActivity.this);

				TextView txtOrder1 = (TextView) alertDialog
						.findViewById(R.id.txt_ordertitle_one);
				txtOrder1.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						startActivity(new Intent(OrderTwoActivity.this,
								OrderOneActivity.class));
						alertDialog.cancel();
						finish();
					}
				});
				TextView txtOrder2 = (TextView) alertDialog
						.findViewById(R.id.txt_ordertitle_two);
				txtOrder2.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						alertDialog.cancel();
					}
				});
				TextView txtOrder3 = (TextView) alertDialog
						.findViewById(R.id.txt_ordertitle_th);
				txtOrder3.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						startActivity(new Intent(OrderTwoActivity.this,
								OrderThActivity.class));
						alertDialog.cancel();
						finish();
					}
				});
			}
		});
		// body
		bodyView = getLayoutInflater().inflate(R.layout.order_main, null);
		layoutBody.addView(bodyView, PyramidManageApplication.layoutParams);

		viewPager = (ViewPager) bodyView.findViewById(R.id.viewpage_order);
		listView = new ArrayList<View>();

		listView.add(childViewInit(1));
		listView.add(childViewInit(2));
		somePagerAdapter = new SomePagerAdapter(listView);
		viewPager.setAdapter(somePagerAdapter);
		butNo1.setBackgroundResource(R.drawable.di1qiu_02);

		linearList = (LinearLayout) bodyView.findViewById(R.id.linear_order);
		linearList.addView(
				getLayoutInflater().inflate(R.layout.order2_list_item, null),
				PyramidManageApplication.layoutParams);
	}

	private View childViewInit(int no) {
		View childView = null;
		switch (no) {
		case 1:
			childView = getLayoutInflater().inflate(R.layout.viewpagechild,
					null);
			butNo1 = (Button) childView.findViewById(R.id.but_child_no1);
			butNo1.setOnClickListener(this);
			butNo2 = (Button) childView.findViewById(R.id.but_child_no2);
			butNo2.setOnClickListener(this);
			butNo3 = (Button) childView.findViewById(R.id.but_child_no3);
			butNo3.setOnClickListener(this);
			butNo4 = (Button) childView.findViewById(R.id.but_child_no4);
			butNo4.setOnClickListener(this);
			butNo5 = (Button) childView.findViewById(R.id.but_child_no5);
			butNo5.setOnClickListener(this);

			butNo1.setBackgroundResource(R.drawable.di1qiu_01);
			butNo2.setBackgroundResource(R.drawable.di2qiu_01);
			butNo3.setBackgroundResource(R.drawable.di3qiu_01);
			butNo4.setBackgroundResource(R.drawable.di4qiu_01);
			butNo5.setBackgroundResource(R.drawable.di5qiu_01);
			break;
		case 2:
			childView = getLayoutInflater().inflate(R.layout.viewpagechild2,
					null);
			butNo6 = (Button) childView.findViewById(R.id.but_child_no6);
			butNo6.setOnClickListener(this);
			butNo7 = (Button) childView.findViewById(R.id.but_child_no7);
			butNo7.setOnClickListener(this);
			butNo8 = (Button) childView.findViewById(R.id.but_child_no8);
			butNo8.setOnClickListener(this);
			butLh = (Button) childView.findViewById(R.id.but_child_nolh);
			butLh.setOnClickListener(this);
			butZh = (Button) childView.findViewById(R.id.but_child_nozh);
			butZh.setVisibility(View.GONE);
			// butZh.setOnClickListener(this);

			butNo6.setBackgroundResource(R.drawable.di4qiu_01);
			butNo7.setBackgroundResource(R.drawable.di5qiu_01);
			butNo8.setBackgroundResource(R.drawable.zhonhlh_01);
			butLh.setBackgroundResource(R.drawable.qzh3_01);
			// butZh.setBackgroundResource(R.drawable.lianma_01);
			break;
		}

		return childView;
	}

	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.but_child_no1) {
			linearList.removeAllViews();
			linearList.invalidate();
			linearList.addView(
					getLayoutInflater()
							.inflate(R.layout.order2_list_item, null),
					PyramidManageApplication.layoutParams);
			butNo1.setBackgroundResource(R.drawable.di1qiu_02);
			butNo2.setBackgroundResource(R.drawable.di2qiu_01);
			butNo3.setBackgroundResource(R.drawable.di3qiu_01);
			butNo4.setBackgroundResource(R.drawable.di4qiu_01);
			butNo5.setBackgroundResource(R.drawable.di5qiu_01);
			butNo6.setBackgroundResource(R.drawable.di4qiu_01);
			butNo7.setBackgroundResource(R.drawable.di5qiu_01);
			butNo8.setBackgroundResource(R.drawable.zhonhlh_01);
			butLh.setBackgroundResource(R.drawable.qzh3_01);
		} else if (id == R.id.but_child_no2) {
			butNo1.setBackgroundResource(R.drawable.di1qiu_01);
			butNo2.setBackgroundResource(R.drawable.di2qiu_02);
			butNo3.setBackgroundResource(R.drawable.di3qiu_01);
			butNo4.setBackgroundResource(R.drawable.di4qiu_01);
			butNo5.setBackgroundResource(R.drawable.di5qiu_01);
			butNo6.setBackgroundResource(R.drawable.di4qiu_01);
			butNo7.setBackgroundResource(R.drawable.di5qiu_01);
			butNo8.setBackgroundResource(R.drawable.zhonhlh_01);
			butLh.setBackgroundResource(R.drawable.qzh3_01);
		} else if (id == R.id.but_child_no3) {
			butNo1.setBackgroundResource(R.drawable.di1qiu_01);
			butNo2.setBackgroundResource(R.drawable.di2qiu_01);
			butNo3.setBackgroundResource(R.drawable.di3qiu_02);
			butNo4.setBackgroundResource(R.drawable.di4qiu_01);
			butNo5.setBackgroundResource(R.drawable.di5qiu_01);
			butNo6.setBackgroundResource(R.drawable.di4qiu_01);
			butNo7.setBackgroundResource(R.drawable.di5qiu_01);
			butNo8.setBackgroundResource(R.drawable.zhonhlh_01);
			butLh.setBackgroundResource(R.drawable.qzh3_01);
		} else if (id == R.id.but_child_no4) {
			butNo1.setBackgroundResource(R.drawable.di1qiu_01);
			butNo2.setBackgroundResource(R.drawable.di2qiu_01);
			butNo3.setBackgroundResource(R.drawable.di3qiu_01);
			butNo4.setBackgroundResource(R.drawable.di4qiu_02);
			butNo5.setBackgroundResource(R.drawable.di5qiu_01);
			butNo6.setBackgroundResource(R.drawable.di4qiu_01);
			butNo7.setBackgroundResource(R.drawable.di5qiu_01);
			butNo8.setBackgroundResource(R.drawable.zhonhlh_01);
			butLh.setBackgroundResource(R.drawable.qzh3_01);
		} else if (id == R.id.but_child_no5) {
			butNo1.setBackgroundResource(R.drawable.di1qiu_01);
			butNo2.setBackgroundResource(R.drawable.di2qiu_01);
			butNo3.setBackgroundResource(R.drawable.di3qiu_01);
			butNo4.setBackgroundResource(R.drawable.di4qiu_01);
			butNo5.setBackgroundResource(R.drawable.di5qiu_02);
			butNo6.setBackgroundResource(R.drawable.di4qiu_01);
			butNo7.setBackgroundResource(R.drawable.di5qiu_01);
			butNo8.setBackgroundResource(R.drawable.zhonhlh_01);
			butLh.setBackgroundResource(R.drawable.qzh3_01);
		} else if (id == R.id.but_child_no6) {
			butNo1.setBackgroundResource(R.drawable.di1qiu_01);
			butNo2.setBackgroundResource(R.drawable.di2qiu_01);
			butNo3.setBackgroundResource(R.drawable.di3qiu_01);
			butNo4.setBackgroundResource(R.drawable.di4qiu_01);
			butNo5.setBackgroundResource(R.drawable.di5qiu_01);
			butNo6.setBackgroundResource(R.drawable.di4qiu_02);
			butNo7.setBackgroundResource(R.drawable.di5qiu_01);
			butNo8.setBackgroundResource(R.drawable.zhonhlh_01);
			butLh.setBackgroundResource(R.drawable.qzh3_01);
		} else if (id == R.id.but_child_no7) {
			butNo1.setBackgroundResource(R.drawable.di1qiu_01);
			butNo2.setBackgroundResource(R.drawable.di2qiu_01);
			butNo3.setBackgroundResource(R.drawable.di3qiu_01);
			butNo4.setBackgroundResource(R.drawable.di4qiu_01);
			butNo5.setBackgroundResource(R.drawable.di5qiu_01);
			butNo6.setBackgroundResource(R.drawable.di4qiu_01);
			butNo7.setBackgroundResource(R.drawable.di5qiu_02);
			butNo8.setBackgroundResource(R.drawable.zhonhlh_01);
			butLh.setBackgroundResource(R.drawable.qzh3_01);
		} else if (id == R.id.but_child_no8) {
			linearList.removeAllViews();
			linearList.invalidate();
			linearList.addView(getLayoutInflater().inflate(
					R.layout.order_listlh_item, null));

			butNo1.setBackgroundResource(R.drawable.di1qiu_01);
			butNo2.setBackgroundResource(R.drawable.di2qiu_01);
			butNo3.setBackgroundResource(R.drawable.di3qiu_01);
			butNo4.setBackgroundResource(R.drawable.di4qiu_01);
			butNo5.setBackgroundResource(R.drawable.di5qiu_01);
			butNo6.setBackgroundResource(R.drawable.di4qiu_01);
			butNo7.setBackgroundResource(R.drawable.di5qiu_01);
			butNo8.setBackgroundResource(R.drawable.zhonhlh_02);
			butLh.setBackgroundResource(R.drawable.qzh3_01);
		} else if (id == R.id.but_child_nolh) {
			linearList.removeAllViews();
			linearList.invalidate();
			linearList.addView(getLayoutInflater().inflate(
					R.layout.order2_listlm_item, null));

			butNo1.setBackgroundResource(R.drawable.di1qiu_01);
			butNo2.setBackgroundResource(R.drawable.di2qiu_01);
			butNo3.setBackgroundResource(R.drawable.di3qiu_01);
			butNo4.setBackgroundResource(R.drawable.di4qiu_01);
			butNo5.setBackgroundResource(R.drawable.di5qiu_01);
			butNo6.setBackgroundResource(R.drawable.di4qiu_01);
			butNo7.setBackgroundResource(R.drawable.di5qiu_01);
			butNo8.setBackgroundResource(R.drawable.zhonhlh_01);
			butLh.setBackgroundResource(R.drawable.qzh3_02);

		}
	}
}